'use strict';

const express = require('express');
const dbutility = require('../../common/dbutility');
const gputility = require('../../common/gputility');
const logger = require('../../common/logutility').logger('regExpInp');
let router = express.Router();

router.get('/', function(req, res) {
    gputility.excuteGP(getGP(req, res));
});

router.post('/', function(req, res) {
    gputility.excuteGP(postGP(req, res));
});

function* getGP(req, res) {
    try {
        res.render('./cou/regExpInp', req.session.expReg);
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

function* postGP(req, res) {
    try {
        let expNo = req.body.expNo;
        let exp = yield dbutility.expModel.getExpByExpNo(expNo);
        if (exp) {
            req.session.expReg = null;
            let pageData = req.body;
            pageData.errorInfo = '该快递已经存在！';
            res.render('./cou/regExpInp', pageData);
        } else {
            req.session.expReg = {};
            req.session.expReg.expNo = expNo;
            req.session.expReg.expReName = req.body.expReName;
            req.session.expReg.expReTel = req.body.expReTel;
            req.session.expReg.expPay = req.body.expPay;
            res.redirect('/regExpConf');
        }
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

module.exports = router;